package jsave.init.xml;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Node;

public class JSaveDocumentXML {

	private static final Logger log = Logger.getLogger(JSaveDocumentXML.class);

	private Document doc;

	public JSaveDocumentXML(Document doc) {

		this.doc = doc;
	}

	public int getNbSauvegardes() {

		List<Node> nodes = doc.selectNodes("/jsave/save");

		log.debug("Nb balises 'save' : " + nodes.size());
		for (int i = 0; i < nodes.size(); i++) {
			Node node = ((Node) nodes.get(i));
			log.debug("Nom savegarde : " + node.valueOf("@nom"));
		}

		return nodes.size();
	}

	public String getNom(int i) {

		Node node = doc.selectSingleNode("/jsave/save[" + i + "]");
		return node.valueOf("@nom");
	}

	public String getSource(int i) {

		Node node = doc.selectSingleNode("/jsave/save[" + i + "]/source");
		return node.getText();
	}

	public String getDestination(int i) {

		Node node = doc.selectSingleNode("/jsave/save[" + i + "]/destination");
		return node.getText();
	}

	public boolean isSimulation(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i
					+ "]/simulation");
			if (node.getText().equalsIgnoreCase("true")) {
				return true;
			}
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return false;
	}

	public boolean isRecursif(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i + "]/recursif");
			if (node.getText().equalsIgnoreCase("true")) {
				return true;
			}
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return false;
	}
	
	public boolean isIncremental(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i + "]/incremental");
			if (node.getText().equalsIgnoreCase("true")) {
				return true;
			}
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return false;
	}

	public String getActionA(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i + "]/actionA");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return null;
	}

	public String getActionB(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i + "]/actionB");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return null;
	}

	public String getActionC(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i + "]/actionC");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return null;
	}

	public String getActionD(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i + "]/actionD");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return null;
	}

	public String getAffichage(int i) {

		try {
			Node node = doc
					.selectSingleNode("/jsave/save[" + i + "]/affichage");
			return node.getText();
		} catch (Exception e) {
			// log.debug(e.getMessage(), e);
		}

		return "ALL";
	}

	public boolean isLogHTML(int i) {

		try {
			Node node = doc.selectSingleNode("/jsave/save[" + i + "]/logHTML");
			if (node.getText().equalsIgnoreCase("true")) {
				return true;
			}
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return false;
	}

	public String getCheminLog(int i) {

		try {
			Node node = doc
					.selectSingleNode("/jsave/save[" + i + "]/cheminLog");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return null;
	}

	public List<String> getExclusions(int i) {

		List<String> exclusions = new ArrayList<String>();
		List<Node> nodes = doc.selectNodes("/jsave/save[" + i + "]/exclusion");

		log.debug("Nb d'exclusions : " + nodes.size());
		for (int j = 0; j < nodes.size(); j++) {
			Node node = ((Node) nodes.get(j));
			exclusions.add(node.getText());
		}

		return exclusions;
	}
	
	public String getRedirectionVers(int i) {

		try {
			Node node = doc
					.selectSingleNode("/jsave/save[" + i + "]/redirection");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}

		return null;
	}
}